var fp;
window.ImgCollapsed = "../resources/images/Collapsed.gif";
window.ImgExpanded = "../resources/images/Expanded.gif";
$(function() {
	moveGapi();
	$.post("../resources/data/gapi.json",function(data) {
		data = j2o(data);
		var pul = "";
		$(data).each(function(i, item) {
				if (item.childul == undefined) {
												pul += "<li><a href='javascript:void(0);' id='togglezk' class='toggleicon' >&nbsp;─&nbsp;</a><a href='javascript:void(0);' fp='"
														+ item.fp
														+ "'>"
														+ item.text
														+ "</a></li>"
											} else {
												var childul = "";
												pul += "<li>"
														+ "<a id='togglezk' class='toggleicon'  href='javascript:void(0);'>&nbsp;─&nbsp;</a><a  href='javascript:void(0);' class='toggleicon'>"+item.text+"</a>"
														+ "<ul class='childul' >";
												$(item.childul)
														.each(
																function(i,
																		childulitem) {
																	childul += "<li>─&nbsp;<a href='javascript:void(0);'  fp='"
																			+ childulitem.fp
																			+ "' >"
																			+ childulitem.text
																			+ "</a></li>";
																});
												pul += childul + "</ul></li>";
											}
										});
						$("#pul").html(pul);
						//节点请求
						$(".childul li a").click(function() {
							requestx(this);
						});
						////节点收起或打开
						$("a[id=togglezk]").click(function() {
							var childul = $(this).next().next(".childul");
							if (childul.html() != undefined) {
								var display = childul.css("display");
								if (display == "block")
									$(this).html("&nbsp;+&nbsp;");
								else
									$(this).html("&nbsp;─&nbsp;");
								$(this).next().next().slideToggle(200);
							}
						});
						//节点收起或打开
						$("a[id=togglezk]").next().click(function() {
							var childul = $(this).next(".childul");
							if (childul.html() != undefined) {
								var display = childul.css("display");
								if (display == "block")
									$(this).prev().html("&nbsp;+&nbsp;");
								else
									$(this).prev().html("&nbsp;─&nbsp;");
								childul.slideToggle(200);
							} else {
								requestx(this);
							}
						});
						//全部收起或打开
						$("#ocpul").click(function(){
							var ocpul=$(this).val();
							if(ocpul=="全部展开"){
								$(this).val("全部收起");
								$(".childul").slideDown(200);
							}else{
								$(this).val("全部展开");
								$(".childul").slideUp(200);
							}
							$("a[id=togglezk]").each(function(i,item){
								if($(item).next().next(".childul").css("display")==undefined||ocpul=="全部展开"){
									$(item).html("&nbsp;─&nbsp;");
								}else{
									$(item).html("&nbsp;+&nbsp;");
								}
							})
						});

					});
	$("#datainit").click(function(){
		sure("陛下，此作若行则无悔之，请三思而后行啊 !",pb);
	});
	$("#api").click(function(){
		$(".gapi").fadeIn(200);
	})

});
//进度条
function pb(){
	progress(function(func){
		$.post("datainit",function(data){
			if(data=="1"){
				func(function(){
					msg("数据初始化成功！");
				});
				
			}else{
				func(function(){
					msg("数据初始化失败！");
				});
			}
		});
	});
		
}
function pbover(){
	$(".cover").fadeOut(200,function(){
		$(".main").foggy(false);
//		$(".childul li a").css("background","none");
		$(".childul li a").css("color","blue");
		$("#inputjson").val("");
		$(".Canvas").html("");
		$("#requrl").html("请求路径: ");
	});
	
}
//请求方法
function go() {
	if($("#inputjson").val()==""){
		msg("请求参数不能为空 !");
		return;
	}
	var param = j2o($("#inputjson").val());
	
	
	var path="../"+fp;
	$.post(path, param, function(data) {
		$("#requrl").text("请求路径: " + fp);
		data = JSON.stringify(data);
		$("#RawJson").text(data);
		Process();
		$(".Canvas").hide();
		$(".Canvas").fadeIn(200);
	})
}
//节点请求
function requestx(obj) {
	fp = $(obj).attr("fp");
	$("#inputjson").load("../resources/data/" + fp + ".json", function(param) {
		if (param.indexOf("Error 404 Not Found") > 0)
			msg("各路诸侯,尔等莫急,且容我三思..");
		else{
			$("#inputjson").val(param);
			go();
			$(".childul li a").css("color","#0064E8");
			$(obj).css("color","#79B900");
		}
	});
}
//json格式化
function format() {
	$("#RawJson").text($("#inputjson").val());
	Process();
}
//api div可移动
function moveGapi(){
	var oDiv = document.getElementById("gapi");
	var dargX = 0;
	var dargY = 0;
	oDiv.onmousedown = function(ev){
	var oEvent = ev || event;
	dargX = oEvent.clientX - oDiv.offsetLeft;//获取鼠标与div左边的距离
	dargY = oEvent.clientY - oDiv.offsetTop;//获取鼠标与div头部的距离
	document.onmousemove = function(ev){
	var oEvent = ev||event;
	var Left = oEvent.clientX - dargX;
	var Top = oEvent.clientY - dargY;
	 
	if (Left<0){
	Left=0;
	}
	if(Left>document.documentElement.clientWidth-oDiv.offsetWidth){
	Left = document.documentElement.clientWidth-oDiv.offsetWidth;
	}
	if (Top<0)
	{
	Top=0;
	}
	if (Top>document.documentElement.clientHeight-oDiv.offsetHeight)
	{
	Top = document.documentElement.clientHeight-oDiv.offsetHeight;
	}
	oDiv.style.left = Left + "px";
	oDiv.style.top = Top + "px"; 
	}
	document.onmouseup = function(){
	document.onmousemove = null;
	document.onmouseup = null;
	};
	return false;
	}
	  }